
use "$base_out/2016t1_FullyLinked_mainTE_own.dta", clear
gen Age=2016-year(DateOfBirth)
keep $controls2 gradename gradename2 d* academyid formerprovince2 stratascore percentscore_Z *T3ET15* *T1DG16*  treatment pupilid period Subject pupils tutors TTR  Age gender academyid2 GradeName2016 Male gradename2
drop if period<=1
drop if period=="T3ET15":periodl
drop if period=="T1DG16":periodl

keep pupilid period percentscore_Z  Subject academyid academyid2 gradename2 formerprovince2 stratascore $controls2 
greshape wide percentscore_Z, i(pupilid period) j(Subject) string

xtset pupilid period
tsfill, full
xfill  academyid academyid2 gradename2 formerprovince2 stratascore $controls2 , i(pupilid)
  
by pupilid: ipolate percentscore_ZEnglish period, gen(int_percentscore_ZEnglish)
by pupilid: ipolate percentscore_ZSwahili period, gen(int_percentscore_ZSwahili)
by pupilid: ipolate percentscore_Zmath period, gen(int_percentscore_Zmath)

greshape long percentscore_Z@ int_percentscore_Z@, i(pupilid period) j(Subject) string
keep if Subject=="Swahili"   | Subject=="math" | Subject=="English"
drop percentscore_Z

merge 1:1 pupilid period Subject using "$base_out/2016t1_FullyLinked_mainTE_own.dta"

keep $controls2 gradename gradename2 formerprovince d* academyid formerprovince2 stratascore int_percentscore_Z* percentscore_Z *T3ET15* *T1DG16*  treatment pupilid period Subject pupils tutors TTR
encode academyid, gen(academyid2)
drop if period<=1
drop if period=="T3ET15":periodl
drop if period=="T1DG16":periodl


*drop if period=="T2ET16":periodl
compress
gen term1=(period=="T1MT16":periodl)
gen term2=(period=="T1ET16":periodl)
gen term3=(period=="T2MT16":periodl)
gen term4=(period=="T2ET16":periodl)
gen term5=(period=="T3MT16":periodl)
drop period



 *pupils
*core pupils

		eststo clear
		foreach subject in $Outcomes_core {
			eststo: reghdfe int_percentscore_Z treatment  $controls $controls_prescores  if pupils==1 & Subject=="`subject'", vce(cluster academyid2) ab((gradename2#formerprovince2#stratascore)##c.(term*))
			estadd ysumm
			qui tab academyid if e(sample)==1 & treatment==0
			estadd scalar controls=r(r)
			estadd local ProvinceFE "Yes"
			estadd local Studentcontrols "Yes"
			estadd local Schoolscontrols "Yes"
			
			local tempm=string(B[1,1], "%9.2gc")
			file open newfile using "$results/LatexCode/Interpolated_coef_`subject'_pupils.tex", write replace
			file write newfile "`tempm'"
			file close newfile
			
			test treatment
			local tempm=string(r(p), "%9.2gc")
			file open newfile using "$results/LatexCode/Interpolated_pvalue_`subject'_pupils.tex", write replace
			file write newfile "`tempm'"
			file close newfile
			
		}
					
		esttab  using "$results/LatexCode/Pooled_pupils_Interpolate_Control.tex", se ar2 booktabs label b(a2) se(a2) nocon nonumber fragment ///
		star(* 0.10 ** 0.05 *** 0.01) ///
		mtitles(Math English Swahili Math English Swahili) ///
		replace keep(treatment) stats(N N_clust , labels("N. of obs." "Number of schools")) ///
		nonotes addnotes("/specialcell{Clustered standard errors, by school, in parenthesis//  /sym{*} /(p<0.10/), /sym{**} /(p<0.05/), /sym{***} /(p<0.01/) }")
		
	
	
	
	**tutors		

		eststo clear
		foreach subject in $Outcomes_core {
			eststo: reghdfe int_percentscore_Z treatment  $controls_old $controls_prescores  if  tutors==1 & Subject=="`subject'", vce(cluster academyid2) ab((gradename2#formerprovince2#stratascore)##c.(term*))
			estadd ysumm
			qui tab academyid if e(sample)==1 & treatment==0
			estadd scalar controls=r(r)
			estadd local ProvinceFE "Yes"
			estadd local Studentcontrols "Yes"
			estadd local Schoolscontrols "Yes"
		}
					
		esttab  using "$results/LatexCode/Pooled_tutors_Interpolate_Control.tex", se ar2 booktabs label b(a2) se(a2) nocon nonumber fragment ///
		star(* 0.10 ** 0.05 *** 0.01) ///
		mtitles(Math English Swahili Math English Swahili) ///
		replace keep(treatment) stats(N N_clust , labels("N. of obs." "Number of schools")) ///
		nonotes addnotes("/specialcell{Clustered standard errors, by school, in parenthesis//  /sym{*} /(p<0.10/), /sym{**} /(p<0.05/), /sym{***} /(p<0.01/) }")
	
